//
//  DLStoreListViewController.m
//  Wetogether
//
//  Created by applex on 14-2-27.
//  Copyright (c) 2014年 sjtu. All rights reserved.
//

#import "TogetherStoreListViewController.h"
#import <SSToolkit/SSToolkit.h>
#import <SSToolkit/SSCategories.h>

@interface TogetherStoreListViewController () <SSWebViewDelegate>

@property (nonatomic, strong) SSWebView *webView;
@property (nonatomic, strong) UIBarButtonItem *rightBarButtonItem;
@property (nonatomic, strong) NSString *currentShopId;

@end

@implementation TogetherStoreListViewController

@synthesize businessName = _businessName;

- (void)setBusinessName:(NSString *)businessId {
    _businessName = businessId;
    [self refresh];
}


- (UIBarButtonItem *)rightBarButtonItem {
    if (!_rightBarButtonItem) {
        _rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(done:)];
    }
    
    return _rightBarButtonItem;
}


- (id)init {
    if ((self = [super init])) {
        _webView = [[SSWebView alloc] init];
    }
    return self;
}


- (void)loadView {
    [super loadView];
    _webView.frame = self.view.bounds;
    _webView.delegate = self;
    [self.view addSubview:_webView];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.leftItemsSupplementBackButton = YES;
    self.navigationItem.rightBarButtonItem = nil;
    self.title = @"相似的店铺";
    
    NSString *keywords = [self.businessName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSString *url = [NSString stringWithFormat:@"http://m.dianping.com/shoplist/1/r/0/c/0/s/s_-1/kw/%@", keywords];
    [self.webView loadURLString:url];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - SSWebViewDelegate 

- (BOOL)webView:(SSWebView *)aWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSString *url = [request.URL absoluteString];
    if ([url containsString:@"shop/"]) {
        self.navigationItem.rightBarButtonItem = self.rightBarButtonItem;
        self.currentShopId = [[url componentsSeparatedByString:@"/"] lastObject];
        return YES;
    } else if (![url isEqualToString:@"about:blank"]) {
        self.navigationItem.rightBarButtonItem = nil;
    }
    return YES;
}


- (void)webViewDidFinishLoad:(SSWebView *)aWebView {
    [aWebView stringByEvaluatingJavaScriptFromString:@"$('#banner').remove();var t = $('[title=\"首页\"]'); if (t.length) {t.parent().append('<img src=\"http://i3.dpfile.com/s/i/app/api/images/app-logo3.06b0ddf70ed8b59a46d7667fcd3f7783.png\" style=\"width:32px; height:32px;margin-top:7px;\" />'); t.remove();}"];
}

#pragma mark - Private Methods

- (void)refresh {
    [self.webView loadHTMLString:self.businessName];
}


- (void)done:(id)sender {
    if (self.currentShopId) {
        [self.viewController didBussinessIdSelected:self.currentShopId forIndexPath:self.indexPath];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

@end
